

package com.fy56.platform.logistics.restful.api.controller.wx;


import com.alibaba.fastjson.JSON;
import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.common.ResultStatus;
import com.fy56.platform.logistics.model.ReceiverContactInfo;
import com.fy56.platform.logistics.restful.api.app.authorization.annotation.Authorization;
import com.fy56.platform.logistics.service.wx.IReceiverContactInfoService;
import com.fy56.platform.logistics.utils.IdWorkerTools;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 收货联系人RestController
 * Created by li on 2018-05-29.
 */
@Api(value = "ReceiverContactInfoManager", description = "收货联系人管理", produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/receiverContactInfo")
public class ReceiverContactInfoController {

    @Autowired
    private IReceiverContactInfoService iReceiverContactInfoService;

    private static Logger log = Logger.getLogger(ReceiverContactInfoController.class);

    @RequestMapping(value = "/search", method = RequestMethod.POST)
    @ApiOperation(value = "查询收货联系人")
    @Authorization
    public ResponseEntity search(@RequestParam String vipMemberId) {
        try {
            return new ResponseEntity(ResultModel.ok(iReceiverContactInfoService.selectByVipMemberId(Long.valueOf(vipMemberId))), HttpStatus.OK);
        } catch (Exception e) {
            log.error("查询收货联系人失败",e);
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, "查询收货联系人失败"), HttpStatus.OK);
        }
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加收货联系人")
    @Authorization
    public ResponseEntity add(@RequestParam String data) {
        ReceiverContactInfo model = null;
        try {
            model = JSON.parseObject(data).toJavaObject(ReceiverContactInfo.class);

            //验证收货联系人是否存在，若收货联系人已存在，提示：该收货联系人已经存在,请重新输入
            if(iReceiverContactInfoService.getExistsByVipMemberIdAndMobile(model) != null){
                return new ResponseEntity(ResultModel.error("收货联系人[" + model.getContactName() + " "
                        + model.getContactMobile() + "]已存在"), HttpStatus.OK);
            }

            //执行插入并返回
            model.setId(Long.valueOf(IdWorkerTools.getInstance().getId()));
            model.setCreateTime(new Date());
            return new ResponseEntity(ResultModel.ok(iReceiverContactInfoService.insert(model)), HttpStatus.OK);

        } catch (Exception e) {
            log.error("添加收货联系人失败",e);
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, "添加收货联系人失败"), HttpStatus.OK);
        } finally {
            model = null;
        }
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "修改收货联系人")
    @Authorization
    public ResponseEntity edit(@RequestParam String data) {
        ReceiverContactInfo model = null;
        try {
            model = JSON.parseObject(data).toJavaObject(ReceiverContactInfo.class);
            model.setUpdateTime(new Date());
            return new ResponseEntity(ResultModel.ok(iReceiverContactInfoService.update(model)), HttpStatus.OK);
        } catch (Exception e) {
            log.error("修改收货联系人失败",e);
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, "修改收货联系人失败"), HttpStatus.OK);
        } finally {
            model = null;
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除收货联系人")
    @Authorization
    public ResponseEntity delete(@RequestParam String id) {
        try {
            return new ResponseEntity(ResultModel.ok(iReceiverContactInfoService.delete(Long.valueOf(id))), HttpStatus.OK);
        } catch (Exception e) {
            log.error("删除收货联系人失败",e);
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, "删除收货联系人失败"), HttpStatus.OK);
        }
    }

}